package com.newsplatform.controller;

import com.newsplatform.common.Result;
import com.newsplatform.entity.Category;
import com.newsplatform.service.CategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 分类控制器
 * 
 * @author Logan.Li
 */
@Tag(name = "分类管理", description = "分类相关接口")
@RestController
@RequestMapping("/api/categories")
@RequiredArgsConstructor
public class CategoryController {
    
    private final CategoryService categoryService;
    
    /**
     * 获取所有分类
     */
    @Operation(summary = "获取所有分类")
    @GetMapping
    public Result<List<Category>> getAllCategories() {
        List<Category> categories = categoryService.list();
        return Result.success(categories);
    }
    
    /**
     * 获取分类详情
     */
    @Operation(summary = "获取分类详情")
    @GetMapping("/{id}")
    public Result<Category> getCategoryDetail(@PathVariable Long id) {
        Category category = categoryService.getById(id);
        if (category == null) {
            return Result.error("分类不存在");
        }
        return Result.success(category);
    }
    
    /**
     * 创建分类
     */
    @Operation(summary = "创建分类")
    @PostMapping
    public Result<Category> createCategory(@Valid @RequestBody Category category, Authentication authentication) {
        try {
            categoryService.save(category);
            return Result.success(category);
        } catch (Exception e) {
            return Result.error("创建分类失败：" + e.getMessage());
        }
    }
    
    /**
     * 更新分类
     */
    @Operation(summary = "更新分类")
    @PutMapping("/{id}")
    public Result<Category> updateCategory(@PathVariable Long id, @Valid @RequestBody Category category, Authentication authentication) {
        try {
            Category existingCategory = categoryService.getById(id);
            if (existingCategory == null) {
                return Result.error("分类不存在");
            }
            
            category.setId(id);
            categoryService.updateById(category);
            return Result.success(category);
        } catch (Exception e) {
            return Result.error("更新分类失败：" + e.getMessage());
        }
    }
    
    /**
     * 删除分类
     */
    @Operation(summary = "删除分类")
    @DeleteMapping("/{id}")
    public Result<Void> deleteCategory(@PathVariable Long id, Authentication authentication) {
        try {
            Category category = categoryService.getById(id);
            if (category == null) {
                return Result.error("分类不存在");
            }
            
            // 检查是否有新闻使用此分类
            if (categoryService.hasNewsInCategory(id)) {
                return Result.error("该分类下还有新闻，无法删除");
            }
            
            categoryService.removeById(id);
            return Result.success();
        } catch (Exception e) {
            return Result.error("删除分类失败：" + e.getMessage());
        }
    }
}
